<?php 
namespace app\admin\controller;
class Content extends Common {

	function index(){
		$channel_id = \mylib\Input::get('channel_id');
		$this -> assign('channel_id', $channel_id);

		$channel = \Ebcms::db() -> get('channel', '*', ['id'=>$channel_id]);
		$this -> assign('channel', $channel);

		$pagenum = $channel['pagenum']?:20;

		$page = \mylib\Input::get('page');
		$page = $page?:1;
		$this -> assign('page', $page);

		$contents = \Ebcms::db() -> select('content', '*', [
			'channel_id' => $channel_id,
			'ORDER' => [
				'id' => 'DESC',
			],
			'LIMIT'=>[($page-1) * $pagenum, $pagenum]
		]);
		foreach ($contents as &$value) {
			$ext = $value['ext'];
			unset($value['ext']);
			$value['attr'] = json_decode($value['attr'], true);
			$value = array_merge(json_decode($ext, true), $value);
		}
		unset($value);
		$this -> assign('contents', $contents);

		$total = \Ebcms::db() -> count('content', [
			'channel_id' => $channel_id,
		]);
		$this -> assign('pages', \Ebcms::page($total, $page, $pagenum));

		$tmp = \Ebcms::db() -> select('field', '*', [
			'model_id' => $channel['model_id'],
			'admin_show' => 1,
			'ORDER' => [
				'weight' => 'ASC',
				'id' => 'ASC',
			]
		]);
		$fields = [];
		foreach ($tmp as $key => $value) {
			$fields[$value['field']] = [
				'title' => $value['title'],
				'type' => $value['type'],
			];
		}
		$this -> assign('fields', $fields);

		return $this -> html($this -> fetch());
	}

	public function add(){
		if (\Ebcms::isPost()) {
			$channel_id = \mylib\Input::post('channel_id');

			$channel = \Ebcms::db() -> get('channel', '*', ['id'=>$channel_id]);
			$tmp = \Ebcms::db() -> select('field', '*', [
				'model_id' => $channel['model_id']
			]);
			$tmp = array_merge($this -> fields(), $tmp);
			$fields = [];
			foreach ($tmp as $key => $value) {
				$fields[$value['field']] = $value;
			}

			$mainfields = ['channel_id','title','thumb','body','jump','tpl','name','click','status','tag','filter1','filter2','filter3','filter4','filter5','filter6'];

			$content = [];
			$ext = [];
			foreach ($_POST as $key => $value) {
				if (in_array($key, $mainfields)) {
					$content[$key] = \mylib\Input::post($key);
				}elseif (isset($fields[$key])) {
					$ext[$key] = \mylib\Input::post($key);
				}
			}
			$content['attr'] = json_encode(\mylib\Input::post('attr'), JSON_UNESCAPED_UNICODE);
			$content['ext'] = json_encode($ext);
			$content['create_time'] = time();
			$content['update_time'] = time();

			\Ebcms::db() -> insert('content', $content);
			$content_id = \Ebcms::db() -> id();

			if ($content['status'] && \mylib\Input::post('attr')) {
				$attrs = [];
				foreach (\mylib\Input::post('attr') as $value) {
					$attrs[] = [
						'channel_id' => $content['channel_id'],
						'content_id' => $content_id,
						'attr' => $value
					];
				}
				\Ebcms::db() -> insert('attr', $attrs);
			}

			if ($content['status'] && $content['tag']) {
				$tmp = explode(' ', $content['tag']);
				$tags = [];
				foreach ($tmp as $value) {
					$tags[] = [
						'content_id' => $content_id,
						'tag' => $value
					];
				}
				\Ebcms::db() -> insert('tag', $tags);
			}

			return $this -> success('操作成功！', \mylib\Route::buildUrl('/admin/content/index') . '?channel_id=' . $channel_id);
		}else{
			$channel_id = \mylib\Input::get('channel_id');
			$this -> assign('channel_id', $channel_id);

			$channel = \Ebcms::db() -> get('channel', '*', ['id'=>$channel_id]);
			$this -> assign('channel', $channel);
			
			$fields = \Ebcms::db() -> select('field', '*', [
				'model_id' => $channel['model_id'],
				'ORDER' => [
					'weight' => 'ASC',
					'id' => 'ASC',
				]
			]);
			$fields = array_merge($this -> fields(), $fields);

			$groups = [];
			foreach ($fields as $key => $value) {
				if (!isset($groups[$value['group']])) {
					$groups[$value['group']] = [];
				}
				$groups[$value['group']][] = $value;
			}
			$this -> assign('groups', $groups);
			return $this -> html($this -> fetch());
		}
	}

	public function edit(){
		if (\Ebcms::isPost()) {
			$content = \Ebcms::db() -> get('content', '*', ['id'=>\mylib\Input::post('id')]);
			$channel_id = $content['channel_id'];

			$channel = \Ebcms::db() -> get('channel', '*', ['id'=>$channel_id]);
			$tmp = \Ebcms::db() -> select('field', '*', [
				'model_id' => $channel['model_id']
			]);
			$tmp = array_merge($this -> fields(), $tmp);
			$fields = [];
			foreach ($tmp as $key => $value) {
				$fields[$value['field']] = $value;
			}

			$mainfields = ['title','thumb','body','jump','tpl','name','click','status','tag','filter1','filter2','filter3','filter4','filter5','filter6'];

			$content = [];
			$ext = [];
			foreach ($_POST as $key => $value) {
				if (in_array($key, $mainfields)) {
					$content[$key] = \mylib\Input::post($key);
				}elseif (isset($fields[$key])) {
					$ext[$key] = \mylib\Input::post($key);
				}
			}
			$content['attr'] = json_encode(\mylib\Input::post('attr'), JSON_UNESCAPED_UNICODE);
			$content['ext'] = json_encode($ext);
			$content['update_time'] = time();

			\Ebcms::db() -> delete('attr', [
				'content_id' => \mylib\Input::post('id')
			]);
			if ($content['status'] && \mylib\Input::post('attr')) {
				$attrs = [];
				foreach (\mylib\Input::post('attr') as $value) {
					$attrs[] = [
						'channel_id' => $channel_id,
						'content_id' => \mylib\Input::post('id'),
						'attr' => $value
					];
				}
				\Ebcms::db() -> insert('attr', $attrs);
			}

			\Ebcms::db() -> delete('tag', [
				'content_id' => \mylib\Input::post('id')
			]);
			if ($content['status'] && $content['tag']) {
				$tmp = explode(' ', $content['tag']);
				$tags = [];
				foreach ($tmp as $value) {
					$tags[] = [
						'content_id' => \mylib\Input::post('id'),
						'tag' => $value
					];
				}
				\Ebcms::db() -> insert('tag', $tags);
			}

			\Ebcms::db() -> update('content', $content, [
				'id'=>\mylib\Input::post('id')
			]);
			return $this -> success('操作成功！', \mylib\Route::buildUrl('/admin/content/index') . '?channel_id=' . $channel_id);
		}else{
			$content = \Ebcms::db() -> get('content', '*', ['id'=>\mylib\Input::get('id')]);

			$ext = json_decode($content['ext'], true);
			unset($content['ext']);

			$content['attr'] = json_decode($content['attr']?:'[]', true);
			$content = array_merge($ext, $content);
			
			$channel = \Ebcms::db() -> get('channel', '*', ['id'=>$content['channel_id']]);
			$this -> assign('channel', $channel);
			
			$fields = \Ebcms::db() -> select('field', '*', [
				'model_id' => $channel['model_id'],
				'ORDER' => [
					'weight' => 'ASC',
					'id' => 'ASC',
				]
			]);
			$fields = array_merge($this -> fields(), $fields);

			$groups = [];
			foreach ($fields as $key => $value) {
				if (!isset($groups[$value['group']])) {
					$groups[$value['group']] = [];
				}
				$groups[$value['group']][] = $value;
			}
			$this -> assign('groups', $groups);

			$this -> assign('content', $content);
			return $this -> html($this -> fetch());
		}
	}

	public function del(){
		if (\Ebcms::isAjax()) {
			\Ebcms::db() -> delete('tag', [
				'content_id' => \mylib\Input::post('id')
			]);
			\Ebcms::db() -> delete('attr', [
				'content_id' => \mylib\Input::post('id')
			]);
			\Ebcms::db() -> delete('content', [
				'id'=>\mylib\Input::post('id'),
			]);
			return $this -> success('操作成功！');
		}
	}

	private function fields(){
		return array (
		  0 => 
		  array (
		    'id' => '1',
		    'model_id' => '1',
		    'group' => '基本信息',
		    'title' => '标题',
		    'field' => 'title',
		    'value' => NULL,
		    'type' => 'text',
		    'config' => '',
		    'tips' => '',
		    'style' => '',
		    'weight' => '1',
		    'admin_show' => '1',
		    'contribute' => '1',
		  ),
		  1 => 
		  array (
		    'id' => '2',
		    'model_id' => '1',
		    'group' => '基本信息',
		    'title' => '缩略图',
		    'field' => 'thumb',
		    'value' => NULL,
		    'type' => 'upload',
		    'config' => '',
		    'tips' => '',
		    'style' => '',
		    'weight' => '2',
		    'admin_show' => '1',
		    'contribute' => '1',
		  ),
		  2 => 
		  array (
		    'id' => '3',
		    'model_id' => '1',
		    'group' => '基本信息',
		    'title' => '内容',
		    'field' => 'body',
		    'value' => NULL,
		    'type' => 'editor',
		    'config' => '',
		    'tips' => '',
		    'style' => '',
		    'weight' => '4',
		    'admin_show' => '0',
		    'contribute' => '1',
		  ),
		  3 => 
		  array (
		    'id' => '4',
		    'model_id' => '1',
		    'group' => '其他信息',
		    'title' => '关键词',
		    'field' => 'keywords',
		    'value' => NULL,
		    'type' => 'text',
		    'config' => '',
		    'tips' => '空格分割，不超过80个字符',
		    'style' => '',
		    'weight' => '99',
		    'admin_show' => '0',
		    'contribute' => '0',
		  ),
		  4 => 
		  array (
		    'id' => '5',
		    'model_id' => '1',
		    'group' => '其他信息',
		    'title' => '内容简介',
		    'field' => 'description',
		    'value' => NULL,
		    'type' => 'textarea',
		    'config' => '',
		    'tips' => '建议不超过200个字符',
		    'style' => '',
		    'weight' => '99',
		    'admin_show' => '0',
		    'contribute' => '0',
		  ),
		  5 => 
		  array (
		    'id' => '6',
		    'model_id' => '1',
		    'group' => '基本信息',
		    'title' => '属性',
		    'field' => 'attr',
		    'value' => NULL,
		    'type' => 'checkbox',
		    'config' => implode(PHP_EOL, explode(' ', \Ebcms::Config('attrs'))),
		    'tips' => '',
		    'style' => '',
		    'weight' => '99',
		    'admin_show' => '1',
		    'contribute' => '0',
		  ),
		  6 => 
		  array (
		    'id' => '7',
		    'model_id' => '1',
		    'group' => '基本信息',
		    'title' => '发布状态',
		    'field' => 'status',
		    'value' => NULL,
		    'type' => 'select',
		    'config' => '0
1',
		    'tips' => '0审核 1发布',
		    'style' => '',
		    'weight' => '99',
		    'admin_show' => '1',
		    'contribute' => '0',
		  ),
		  7 => 
		  array (
		    'id' => '9',
		    'model_id' => '1',
		    'group' => '其他信息',
		    'title' => '跳转链接',
		    'field' => 'jump',
		    'value' => NULL,
		    'type' => 'text',
		    'config' => '',
		    'tips' => '',
		    'style' => '',
		    'weight' => '99',
		    'admin_show' => '0',
		    'contribute' => '0',
		  ),
		  8 => 
		  array (
		    'id' => '10',
		    'model_id' => '1',
		    'group' => '其他信息',
		    'title' => '内容模板',
		    'field' => 'tpl',
		    'value' => NULL,
		    'type' => 'text',
		    'config' => '',
		    'tips' => '',
		    'style' => '',
		    'weight' => '99',
		    'admin_show' => '0',
		    'contribute' => '0',
		  ),
		  9 => 
		  array (
		    'id' => '11',
		    'model_id' => '1',
		    'group' => '其他信息',
		    'title' => '文件名',
		    'field' => 'name',
		    'value' => NULL,
		    'type' => 'text',
		    'config' => '',
		    'tips' => '',
		    'style' => '',
		    'weight' => '99',
		    'admin_show' => '0',
		    'contribute' => '0',
		  ),
		  10 => 
		  array (
		    'id' => '12',
		    'model_id' => '1',
		    'group' => '其他信息',
		    'title' => '标签',
		    'field' => 'tag',
		    'value' => NULL,
		    'type' => 'text',
		    'config' => '',
		    'tips' => '用空格分割',
		    'style' => '',
		    'weight' => '99',
		    'admin_show' => '0',
		    'contribute' => '0',
		  ),
		);
	}
}